﻿using HY.Common;
using HY.Entity;
using HY.Service;
using HY.Web.Controllers;
using HY.Web.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace HY.Web.Areas.Vehicle.Controllers
{
    public class TrajectoryController : BaseController
    {
        // GET: Vehicle/Trajectory
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 获取GPS数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GPSData()
        {
            GPSDataService dataService = new GPSDataService();
            ReturnModel returnModel = new ReturnModel();
            try
            {
                var gpsDatas = dataService.EFGetMany<GPSData>(e => e.VehicleType == "豫A016DW").OrderBy(o => o.ID).To2DArray(t => t.Longitude, t => t.Latitude);
                returnModel = base.GetReturnModel(gpsDatas.Length, gpsDatas);
            }
            catch (Exception ex)
            {
                returnModel = base.GetReturnModel(ex);
                LogHelper.Error("获取GPS数据", ex);
            }
            return Json(returnModel);
        }

        public ActionResult Index2()
        {
            return View();
        }
        [HttpPost]
        public JsonResult GPSData2()
        {
            GPSDataService dataService = new GPSDataService();
            ReturnModel returnModel = new ReturnModel();
            try
            {
                var gpsDatas = dataService.EFGetMany<GPSData>(e => e.VehicleType == "湖南016DW").OrderBy(o => o.ID);
                var gpsStr = GetMessage(gpsDatas);
                returnModel = base.GetReturnModel(gpsDatas.Count(), gpsStr);
            }
            catch (Exception ex)
            {
                returnModel = base.GetReturnModel(ex);
                LogHelper.Error("获取GPS数据", ex);
            }
            return Json(returnModel);


            string GetMessage(IEnumerable<GPSData> datas)
            {
                double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
                StringBuilder sb = new StringBuilder();
                foreach (var item in datas)
                {
                    double bd_lon; //lng  // 经度
                    double bd_lat; //lat  // 纬度
                    double x = (double)item.Longitude, y = (double)item.Latitude;
                    double z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * x_pi);
                    double theta = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * x_pi);
                    bd_lon = z * Math.Cos(theta) + 0.0065;
                    bd_lat = z * Math.Sin(theta) + 0.006;
                    double times = ConvertDateTimeIntsss(item.LocationTime);//当前时间（utc时间）
                    sb.Append(bd_lon + ",");//经度
                    sb.Append(bd_lat + ",");//纬度
                                            //sb.Append(Longitude + ",");//经度
                                            //b.Append(Latitude + ",");//纬度
                    sb.Append(times + ",");//时间
                    sb.Append(item.Speed + ";");//车速

                }
                return sb.ToString();
            }
            long ConvertDateTimeIntsss(DateTime time)
            {
                DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0, 0));
                long t = (time.Ticks - startTime.Ticks) / 10000;//除10000调整为13位
                return t;
            }
        }
    }
}